define(function() {
	return function($compile, $scope) {
		require(
				[ "PDUtilDir/dataService", "AppPathDir/product/productInfos/productSupport",
						"AppPathDir/product/productInfos/addSupport",
						"AppPathDir/product/productInfos/updateSupport",
						"PDUtilDir/util", "PDUtilDir/inputSelect", "Date",
						"DateCN", "css!DateCss",
						"css!AppPathDir/product/productInfos/views/css/css" ],
				function(DataService, Support, AddSupport, UpdateSupport, Util, inputSelect) {
					/* 查询 start */
					$scope.search = {};
					$scope.cache = {
						types : new Array(),
						typeMap: new Map(),
						saleStatus: new Array(),
						saleStatusMap: new Map(),
						provinces : new Array()
					};
					$scope.searchSupport = {
						run : function() {
							$scope.grid.reload({
								queryParam : {
									model: JSON.stringify($scope.search)
								}
							})
						}
					};
					//获取类型信息
					$.ajax({
						url : getServer() + "/flea/hclv/getTourism.htmls",
						success : function(data) {
                            if (data.status != 200) {
                                return;
                            }
							$scope.cache.types = data.data;
							$.each(data.data, function(index, row) {
								$scope.cache.typeMap.set(row.dictCode, row.dictName);
							});
							$scope.renderGridFun();
						}
					});
					//获取省列表
					$.ajax({
						url : getServer() + "/flea/hclv/getProvinces.htmls",
						success : function(data) {
                            if (data.status != 200) {
                                return;
                            }
							$scope.cache.provinces = data.data;
							init_startPlaceProvince();
							init_endPlaceProvince();
						}
					});
					//获取销售状态
					$.ajax({
						url : getServer() + "/flea/hclv/getSaleStatus.htmls",
						success : function(data) {
                            if (data.status != 200) {
                                return;
                            }
							$scope.cache.saleStatus = data.data;
							init_saleStatus();
							$.each(data.data, function(index, row) {
								$scope.cache.saleStatusMap.set(row.dictCode, row.dictName);
							});
							$scope.renderGridFun();
						}
					});
					//获取城市信息
					$scope.getCity = function(type, pcode) {
						$.ajax({
							url : getServer() + "/flea/hclv/getCity.htmls",
							data : {
								'pcode' : pcode
							},
							success : function(data) {
                                if (data.status != 200) {
                                    return;
                                }
								var citys = data.data;
								if (type == "start") {
									$scope.cache.startPlaceCity = data.data;
									init_startPlaceCity();
								} else {
									$scope.cache.endPlaceCity = data.data;
									init_endPalceCity();
								}
							}
						});
					};
					//出发省下拉框初始化
					var init_startPlaceProvince = function() {
						// 初始化下拉框
						inputSelect.dropCache();
						inputSelect({
							id : "startPlaceProvince",
							key : {
								id : "dictCode",
								name : "dictName",
								data : "data",
							},
							data : $scope.cache.provinces,
							simpleData : true,
							// 启用文本搜索
							// initData:[initProvice],
							searchAble : false,
							callback : function(id) {
								var id = id[0];
								$scope.search.startPlaceProvince = id;
								$scope.getCity('start', id);
							}
						});
					}, 
					//出发城市下拉框初始化
					init_startPlaceCity = function() {
						// 初始化下拉框
						inputSelect.dropCache();
						inputSelect({
							id : "startPlaceCity",
							key : {
								id : "dictCode",
								name : "dictName",
								data : "data",
							},
							data : $scope.cache.startPlaceCity,
							simpleData : true,
							// 启用文本搜索
							// initData:[initProvice],
							searchAble : false,
							callback : function(id) {
								var id = id[0];
								$scope.search.startPlaceCity = id;
							}
						});
					}, 
					//目的省下拉框初始化
					init_endPlaceProvince = function() {
						// 初始化下拉框
						inputSelect.dropCache();
						inputSelect({
							id : "endPlaceProvince",
							key : {
								id : "dictCode",
								name : "dictName",
								data : "data",
							},
							data : $scope.cache.provinces,
							simpleData : true,
							// 启用文本搜索
							// initData:[initProvice],
							searchAble : false,
							callback : function(id) {
								var id = id[0];
								$scope.search.endPlaceProvince = id;
								$scope.getCity('end', id);
							}
						});
					}, 
					//目的城市下拉框初始化
					init_endPalceCity = function() {
						// 初始化下拉框
						inputSelect.dropCache();
						inputSelect({
							id : "endPalceCity",
							key : {
								id : "dictCode",
								name : "dictName",
								data : "data",
							},
							data : $scope.cache.endPlaceCity,
							simpleData : true,
							// 启用文本搜索
							// initData:[initProvice],
							searchAble : false,
							callback : function(id) {
								var id = id[0];
								$scope.search.endPalceCity = id;
							}
						});
					},
					//销售状态下拉框初始化
					init_saleStatus = function() {
						// 初始化下拉框
						inputSelect.dropCache();
						inputSelect({
							id : "saleStatus",
							key : {
								id : "dictCode",
								name : "dictName",
								data : "data",
							},
							data : $scope.cache.saleStatus,
							simpleData : true,
							// 启用文本搜索
							// initData:[initProvice],
							searchAble : false,
							callback : function(id) {
								var id = id[0];
								$scope.search.saleStatus = id;
							}
						});
					};
					// 类别下拉框初始化
					inputSelect.dropCache();
					inputSelect({
						id : "productType",
						key : {
							id : "dictCode",
							name : "dictName",
							data : "data",
						},
						data : [ {
							"dictCode" : "self",
							"dictName" : "自营"
						}, {
							"dictCode" : "unself",
							"dictName" : "非自营"
						} ],
						simpleData : true,
						// 启用文本搜索
						// initData:[initProvice],
						searchAble : false,
						callback : function(id) {
							var id = id[0];
							$scope.search.productType = id;
						}
					});
					//出发日期初始化
					$("#departureDate").datetimepicker({
						// 设置使用语言：cn是自定义的中文版本，还可以扩展其他语言版本
						language : "cn",
						// 输出格式化
						// format : 'yyyy-mm-dd hh:ii:ss',
						format : 'yyyy-mm-dd',
						// 直接选择‘今天’
						todayBtn : true,
						// 设置最精确的时间选择视图
						minView : 'month',
						// 高亮当天日期
						todayHighlight : true,
						// 显示“上午”“下午”
						showMeridian : true,
						// 选择完毕后自动关闭
						autoclose : true
					});
					/* 查询 end */
					// 添加
					$scope.addSupport = AddSupport($scope, $compile);
					// 修改
					$scope.updateSupport = UpdateSupport($scope, $compile);
					
					//上架
					$scope.upFun = function(id) {
						Util.confirm("是否要上架？", function() {
							$.ajax({
								url : getServer() + "/flea/hclv/product/updateToOnSale.htmls",
								method : 'post',
								data : {
									'ids' : JSON.stringify([id])
								},
								success : function(data) {
									if(data.status != 200){
										return ;
									}
									$scope.grid.refresh();
									Util.alert(data.message);
								}
							});
						});
					};
					//下架
					$scope.downFun = function(id) {
						Util.confirm("是否要下架？", function() {
							$.ajax({
								url : getServer() + "/flea/hclv/product/updateToOndown.htmls",
								method : 'post',
								data : {
									'ids' : JSON.stringify([id])
								},
								success : function(data) {
									if(data.status != 200){
										return ;
									}
									$scope.grid.refresh();
									Util.alert(data.message);
								}
							});
						});
					};
					//导出
					$scope.exportFun = function(id) {
						Util.confirm("确认导出？", function() {
							 var url = getServer() + "/flea/hclv/product/exportWord.htmls?productId=" + id;
						        form = $("<form></form>").attr("action", url).attr("method", "post");
						        form.appendTo('body').submit().remove();
						});
					};
					// 班期
                    $scope.timetableSupport = {
                        run : function(id) {
                          var allData = $scope.grid.getAllData(), data = undefined;
                          $.each(allData, function(index, row) {
                            if(row.id == id){
                              data = row;
                              DataService.setObject('product.object', data);
                            }
                          });
                            window.top.tabs
                                .addTab({
                                    id : id + '_timetable',
                                    title : "【" + data.name + "】班期管理",
                                    remote : {
                                        url : getServer()
                                        + "/static/core/page/home/subHome.jsp",
                                        iframe : true,
                                        param : {
                                            html : getServer()
                                            + "/static/app/product/productInfos/views/timetable.html",
                                            ctrl : getServer()
                                            + "/static/app/product/productInfos/timetable.js",
											id: id
                                        }
                                    }
                                });
                        }
                    };

					// 删除
					$scope.delSupport = {
						run : function() {
							var selectRows = $scope.grid.getSelectedRow();
							if (selectRows.length == 0) {
								Util.alert('请选择至少一条的数据！');
								return;
							}
							Util.confirm("是否要删除", function() {
								var ids = new Array();
								$.each(selectRows, function(index, selectRow) {
									ids.push(selectRow.id);
								});
								$.ajax({
									url : getServer() + "/flea/hclv/product/delete.htmls",
									method : 'post',
									data : {
										'ids' : JSON.stringify(ids)
									},
									success : function(data) {
										if(data.status != 200){
											return ;
										}
										$scope.grid.refresh();
										Util.alert(data.message);
									}
								});
							});
						}
					};
					//列表初始化
					$scope.renderGridFun = function(){
						if($scope.cache.saleStatus.length > 0 && $scope.cache.types.length > 0){
							$scope.grid = Support.init($scope, $scope.addSupport,
									$scope.updateSupport, $scope.delSupport,
									$scope.searchSupport);
						}
					}
				})
	}
});